﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace IntermediateServiceExample
{
    [ServiceContract(Name="IntermediateServiceManager", Namespace="http://Example/IntermediateService")]
    public interface IIntermediateServiceContract
    {
        [OperationContract(Name="ProcessMessage",Action="*",ReplyAction="*")]
        Message ProcessMessage(Message message);
    }

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, AddressFilterMode = AddressFilterMode.Any)]
    public class IntermediateServiceManager : IIntermediateServiceContract
    {
        #region IIntermediateServiceContract Members

        public Message ProcessMessage(Message requestMessage)
        {
            ChannelFactory<IIntermediateServiceContract> factory = new ChannelFactory<IIntermediateServiceContract>("client");
            IIntermediateServiceContract proxy = factory.CreateChannel();


            IClientChannel clientChannel = proxy as IClientChannel;
            Console.WriteLine(String.Format("Request received at {0}, to {1}", DateTime.Now, clientChannel.RemoteAddress.Uri.AbsoluteUri));


            Message responseMessage = proxy.ProcessMessage(requestMessage);

            Console.WriteLine(String.Format("Reply received at {0}", DateTime.Now));
            Console.WriteLine();
            return responseMessage;
        }

        #endregion
    }
}
